package com.example.zsydb.controller;


import com.example.zsydb.repository.PumpRepository;
import com.example.zsydb.repository.ResultRepository;
import com.example.zsydb.service.ResultService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.zsydb.service.PythonService;
import org.springframework.http.ResponseEntity;

import java.time.LocalDateTime;
import java.util.*;

@RestController
@RequestMapping(value = "/api/pumpjack", produces = "application/json;charset=UTF-8")
//http://localhost:8080/swagger-ui.html
public class PumpjackViewController {

    private final PumpRepository pRepository;
    private final PythonService pythonService;
    private final ResultRepository resultRepository;
    private final ResultService resultService;
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    public PumpjackViewController(PumpRepository pRepository,
                                  PythonService pythonService,
                                  ResultRepository resultRepository,
                                  ResultService resultService) {
        this.pRepository = pRepository;
        this.pythonService = pythonService;
        this.resultRepository = resultRepository;
        this.resultService = resultService;
    }
//    value = "/data", produces = "application/json;charset=UTF-8"
    @GetMapping(value = "/data", produces = "application/json;charset=UTF-8")
    public ResponseEntity<?> getPumpjackData(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "99999") int size,
            @RequestParam(required = false) String JH) {


        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("筛选数据: " + now);

        List<Map<String, Object>> data = pRepository.findAll(page, size, JH);
        //打印data
        System.out.println(data);
        try {
            String pythonResult = pythonService.executePythonScript(data);

            // 新增保存逻辑
            JsonNode rootNode = objectMapper.readTree(pythonResult);
            JsonNode dataNode = rootNode.get("data");
            if (dataNode != null && dataNode.isArray()) {
                for (JsonNode item : dataNode) {
                    resultRepository.saveResult(
                            item.get("JH").asText(),
                            item.get("GTZD").asText(),
                            item.get("DYM").asText(),
                            item.get("CL").asText(),
                            item.get("ID").asText(),
                            item.has("STROKE") ? item.get("STROKE").asDouble() : null,
                            item.has("FREQUENCY") ? item.get("FREQUENCY").asDouble() : null,
                            item.has("SUSP_MAX_LOAD") ? item.get("SUSP_MAX_LOAD").asDouble() : null,
                            item.has("SUSP_MIN_LOAD") ? item.get("SUSP_MIN_LOAD").asDouble() : null,
                            item.has("UPGOING_MAX_CURRENT") ? item.get("UPGOING_MAX_CURRENT").asDouble() : null,
                            item.has("DOWNGOING_MAX_CURRENT") ? item.get("DOWNGOING_MAX_CURRENT").asDouble() : null,
                            item.has("GGGF") ? item.get("GGGF").asDouble() : null,
                            item.has("GGCD") ? item.get("GGCD").asDouble() : null,
                            item.has("CYGGF1") ? item.get("CYGGF1").asDouble() : null,
                            item.has("CYGCD1") ? item.get("CYGCD1").asDouble() : null,
                            item.has("CYGGF2") ? item.get("CYGGF2").asDouble() : null,
                            item.has("CYGCD2") ? item.get("CYGCD2").asDouble() : null,
                            item.has("YY") ? item.get("YY").asDouble() : null,
                            item.has("TY") ? item.get("TY").asDouble() : null,
                            item.has("HS") ? item.get("HS").asDouble() : null,
                            item.has("BJ") ? item.get("BJ").asDouble() : null,
                            item.has("BS") ? item.get("BS").asDouble() : null,
                            item.get("DYNA_CREATE_TIME").asText(),
                            item.get("CHECK_DATE").asText()
                    );
                    // 新增保存到 jh_date_result 表的逻辑
//                    resultRepository.saveJhDateResult(
//                            item.get("JH").asText(),
//                            item.get("DYNA_CREATE_TIME").asText()
//                    );
                }
            }

            return ResponseEntity.ok().body(pythonResult);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(
                    Map.of("error", "处理失败: " + e.getMessage())
            );
        }
    }

    // 新增分页查询接口
    @GetMapping(value = "/results", produces = "application/json;charset=UTF-8")
    public ResponseEntity<?> getResultData(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        if (page < 1 || size < 1) {
            return ResponseEntity.badRequest().body(
                    Map.of("error", "分页参数无效：页码和大小必须大于0")
            );
        }

        try {
            Map<String, Object> result = resultService.getResults(page, size);
            return ResponseEntity.ok().body(result);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(
                    Map.of("error", "查询失败: " + e.getMessage())
            );
        }
    }
}